package com.xu.drools.rule.rule1;

import com.alibaba.fastjson.JSON;
import com.xu.drools.bean.Person;
import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.QueryResults;
import org.kie.api.runtime.rule.QueryResultsRow;

/**
 * 使用kmodule的方式调用drools
 * /resources/META-INF/kmodule.xml
 */
public class rule1 {
    public static void main(final String[] args) {
        KieContainer kc = KieServices.Factory.get().getKieClasspathContainer();

        System.out.println(kc.verify().getMessages().toString());
        execute(kc);
    }

    private static void execute(KieContainer kc) {
        KieSession ksession = kc.newKieSession("rule1KS");
        Person p = new Person();
        p.setAge(19);
        ksession.insert(p);

        ksession.fireAllRules();

        QueryResults results = ksession.getQueryResults("people2");
        System.out.println("results size is " + results.size());
        for(QueryResultsRow row : results){
            Person person = (Person) row.get("person");
            System.out.println("Person from WM, age : " + person.getAge());
        }

        ksession.dispose();
    }
}
